#include <bits/stdc++.h>
#define int long long
using namespace std;
int T,n,f[1000006],g[1000006],InF=1e17,mxf,mxg,tf,tg,ans,a[1000006];
signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>T;
    while(T--){
        cin>>n;ans=0;
        for(int i=1;i<=1000000;i++)f[i]=g[i]=-InF;
        mxf=mxg=tf=tg=0;
        f[0]=g[0]=0;
        for(int i=1;i<=n;i++){
            cin>>a[i];
            int x=max(mxf,f[a[i]]+a[i]+tf),y=max(mxg,g[a[i]]+a[i]+tg);
            if(a[i]==a[i-1])tf+=a[i],tg+=a[i],mxf+=a[i],mxg+=a[i];
            mxf=max(mxf,y);f[a[i-1]]=max(f[a[i-1]],y-tf);
            mxg=max(mxg,x);g[a[i-1]]=max(g[a[i-1]],x-tg);
            ans=0;
            // if(T==9){
        // for(int i=0;i<=1000000;i++)ans=max(ans,f[i]),ans=max(ans,g[i]);
        // cout<<ans<<endl;}
        }
        // for(int i=0;i<=1000000;i++)ans=max(ans,f[i]),ans=max(ans,g[i]);
        cout<<max(mxf,mxg)<<endl;
    }
    return 0;
}